import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class StockPickingDialog extends StatefulWidget {
  StockPickingDialog();

  @override
  _StockPickingDialogState createState() => _StockPickingDialogState();
}

class _StockPickingDialogState extends State<StockPickingDialog> {

  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  StockPicking  stockPicking = StockPicking();
  late List<SelectOptionVO> locationtypeList = [];

  DateFormat format= DateFormat('yyyy-MM-dd');

  @override
  void initState() {
    super.initState();
    stockPicking.createDateStart = Utils.getDate();
    stockPicking.createDateEnd = Utils.getDate();
    _loadData();
  }

  _loadData() async{
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      locationtypeList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: "comment",
          val: 'XBK'
      );
    }else {
      locationtypeList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: "comment",
          val: 'XBK',
          key2: 'workshop_id',
          val2: int.parse(userInfo.deptId!)
      );
    }
    setState(() { });
  }
  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            label: '开始时间',
            value: stockPicking.createDateStart,
            lastDate: isNotBlank(stockPicking.createDateEnd)?format.parse(stockPicking.createDateEnd!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              stockPicking.createDateStart = v;
              setState(() {});
            },
            onSaved: (v) {
              stockPicking.createDateStart = v;
            },
          ),
          CrySelectDate(
            context,
            label: '结束时间',
            value: stockPicking.createDateEnd,
            firstDate: isNotBlank(stockPicking.createDateStart)?format.parse(stockPicking.createDateStart!):null,
            onDateChange: (v) {
              formKey.currentState?.save();
              stockPicking.createDateEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              stockPicking.createDateEnd = v;
            },
          ),
          CrySelect(
            label: '源库房',
            dataList: locationtypeList,
            value: stockPicking.locationTypeId,
            onSaved: (v) => {stockPicking.locationTypeId = v},
          ),
          CryInput(
            label: '入库单号',
            value: stockPicking.name,
            onSaved: (v) {
              stockPicking.name = v;
            },
          ),
          CryInput(
            label: '生产单号',
            value: stockPicking.productorderno,
            onSaved: (v) {
              stockPicking.productorderno = v;
            },
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, stockPicking);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}